package com.farm.learn.controller;

import com.farm.core.auth.domain.LoginUser;
import com.farm.core.page.OperateType;
import com.farm.core.page.RequestMode;
import com.farm.core.page.ViewMode;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.learn.domain.ClassUser;
import com.farm.learn.domain.Classt;
import com.farm.learn.domain.ex.ClassView;
import com.farm.learn.service.ClassUserServiceInter;
import com.farm.learn.service.ClasstServiceInter;
import com.farm.web.WebUtils;
import com.farm.web.easyui.EasyUiUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


@RequestMapping("/classuser")
@Controller
public class ClassUserController extends WebUtils {
    private final static Logger log = Logger.getLogger(ClassUserController.class);
    @Resource
    ClassUserServiceInter classUserServiceImpl;
    @Resource
    private ClasstServiceInter classTServiceImpl;

    public ClassUserServiceInter getClassUserServiceImpl() {
        return classUserServiceImpl;
    }

    public void setClassUserServiceImpl(ClassUserServiceInter classUserServiceImpl) {
        this.classUserServiceImpl = classUserServiceImpl;
    }

    @RequestMapping("/query")
    @ResponseBody
    public Map<String, Object> queryall(DataQuery query,
                                        HttpServletRequest request, HttpSession session) {

        try {
            LoginUser currentUser = getCurrentUser(session);
            query = EasyUiUtils.formatGridQuery(request, query);
            query.addRule(new DBRule("CUSER", currentUser.getId(), "="));
            DataResult result = classUserServiceImpl
                .createClassUserSimpleQuery(query).search();
            List<Map<String, Object>> list = result.getResultList();
            for (Map<String, Object> map : list) {
                String classid = map.get("CLASSID").toString();
                ClassView classView = classTServiceImpl.getClassView(classid, currentUser);
                map.put("classView", classView);
            }
            return ViewMode.getInstance()
                .putAttrs(EasyUiUtils.formatGridData(result))
                .returnObjMode();
        } catch (Exception e) {
            log.error(e.getMessage());
            return ViewMode.getInstance().setError(e.getMessage(), e)
                .returnObjMode();
        }
    }


    @RequestMapping("/edit")
    @ResponseBody
    public Map<String, Object> editSubmit(ClassUser entity, HttpSession session) {

        try {
            entity = classUserServiceImpl.editClassUserEntity(entity,
                getCurrentUser(session));
            return ViewMode.getInstance().setOperate(OperateType.UPDATE)
                .putAttr("entity", entity).returnObjMode();

        } catch (Exception e) {
            log.error(e.getMessage());
            return ViewMode.getInstance().setOperate(OperateType.UPDATE)
                .setError(e.getMessage(), e).returnObjMode();
        }
    }


    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> addSubmit(ClassUser entity, HttpSession session) {

        try {
            String classid = entity.getClassid();
            DataQuery query = new DataQuery();
            query.addRule(new DBRule("CLASSID", classid, "="));
            DataResult result = classUserServiceImpl
                .createClassUserSimpleQuery(query).search();
            int size = result.getResultList().size();
            Classt classtEntity = classTServiceImpl.getClasstEntity(classid);
            if (size + 1 > classtEntity.getVolume()) {
                throw new RuntimeException("该课程最多报名" + classtEntity.getVolume() + "人，已报满！");
            }
            entity = classUserServiceImpl.insertClassUserEntity(entity,
                getCurrentUser(session));
            return ViewMode.getInstance().setOperate(OperateType.ADD)
                .putAttr("entity", entity).returnObjMode();
        } catch (Exception e) {
            log.error(e.getMessage());
            return ViewMode.getInstance().setOperate(OperateType.ADD)
                .setError(e.getMessage(), e).returnObjMode();
        }
    }


    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> delSubmit(String ids, HttpSession session) {
        try {
            for (String id : parseIds(ids)) {
                classUserServiceImpl.deleteClassUserEntity(id,
                    getCurrentUser(session));
            }
            return ViewMode.getInstance().returnObjMode();
        } catch (Exception e) {
            log.error(e.getMessage());
            return ViewMode.getInstance().setError(e.getMessage(), e)
                .returnObjMode();
        }
    }

    @RequestMapping("/list")
    public ModelAndView index(HttpSession session) {
        return ViewMode.getInstance()
            .returnModelAndView("learn/ClassUserResult");
    }

    @RequestMapping("/form")
    public ModelAndView view(RequestMode pageset, String ids) {
        try {
            switch (pageset.getOperateType()) {
                case (0): {//查看
                    return ViewMode.getInstance().putAttr("pageset", pageset)
                        .putAttr("entity", classUserServiceImpl.getClassUserEntity(ids))
                        .returnModelAndView("question/ClassUserForm");
                }
                case (1): {// 新增
                    return ViewMode.getInstance().putAttr("pageset", pageset)
                        .returnModelAndView("question/ClassUserForm");
                }
                case (2): {//修改
                    return ViewMode.getInstance().putAttr("pageset", pageset)
                        .putAttr("entity", classUserServiceImpl.getClassUserEntity(ids))
                        .returnModelAndView("question/ClassUserForm");
                }
                default:
                    break;
            }
            return ViewMode.getInstance().returnModelAndView("question/ClassUserForm");
        } catch (Exception e) {
            return ViewMode.getInstance().setError(e.getMessage(), e)
                .returnModelAndView("question/ClassUserForm");
        }
    }
}
